import Request from './request'

const URL_MAP = {
  // 登录授权
  LOGIN: '/login/login',
  // 获取用户基本信息(配置)
  GET_USER_FIELD_CONFIG: '/pub/index',
  // 个人用户信息
  GET_PERSONAL_INFO: '/personal/userinfo',
  // 更新用户信息
  UPDATE_PERSONAL_INFO: '/personal/userEdit',
  // 获取用户列表
  GET_USER_LIST: '/index/userList',
  // 获取他人用户信息
  GET_USER_INFO: '/index/info',
  // 访问记录
  GET_PERSONAL_ACCESS_LIST: '/personal/accessList',
  // 收藏/取消收藏
  UPDATE_COLLECT: '/personal/collectAdd',
  // 收藏记录
  GET_COLLECT_LIST: '/personal/collectList',
  // 拉手(查看微信)
  ADD_HAND_INFO: '/personal/handAdd',
  // 拉手记录
  GET_HAND_LIST: '/personal/handList',
  // 活动列表
  GET_ACTIVITY_LIST: '/index/activityList',
  // 广告信息
  GET_AD_LIST: '/pub/advestList',
  // 会员等级信息
  GET_VIP_CONFIG: '/personal/gradeList',
  // 上传图片
  UPLOAD_IMAGE: '/pub/uploadImage',
  // 添加订单
  ADD_ORDER: '/personal/ordersAdd',
  // 支付
  PAY: '/pay/pay',
  // 碎片信息(配置信息)
  GET_PUBLIC_INFO: '/pub/info',
  // 所有城市
  GET_ALL_CITY: '/pub/getAllCity',
}

/**
 * 登录
 * @param {string} phone_code 换取手机号凭证
 */
export const useLogin = async (phone_code: string) => {
  return new Promise((resolve, reject) => {
    wx.login({
      async success(res) {
        if (res.code) {
          // let address_ip = ''
          // try {
          //   address_ip = await getLocateCityName()
          // } catch (e) {
          //   address_ip = '未知'
          // }
          const body = {
            code: res.code,
            code1: phone_code,
            // code: phone_code,
            // code1: res.code,
            // encryptedData: encodeURIComponent(encryptedData),
            // iv: encodeURIComponent(iv),
            // address_ip,
          }
          try {
            wx.showLoading({
              title: '登录中',
              mask: true,
            })
            Request.post(URL_MAP.LOGIN, body).then((result) => {
              console.log('登录结果', result)
              if (result.code === 10000) {
                wx.showToast({
                  icon: 'success',
                  title: '登录成功',
                })
                const { token, open_id, phone } = result.data
                wx.setStorageSync('token', token)
                wx.setStorageSync('open_id', open_id)
                wx.setStorageSync('phone', phone)
                const loginResult = {
                  token,
                  open_id,
                  phone,
                }

                wx.showLoading({
                  title: '获取用户信息',
                  duration: 0,
                  mask: true,
                })
                Request.post(URL_MAP.GET_PERSONAL_INFO, {})
                  .then((userinfo: any) => {
                    if (userinfo.code === 10000) {
                      // userinfo.data.head_img = DEFAULT_AVATAR
                      // userinfo.data.nickname = desensitizePhoneNumber(phone)
                      wx.setStorageSync('userInfo', JSON.stringify(userinfo.data))
                      wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 600,
                      })
                      resolve(loginResult)
                    } else {
                      reject(userinfo.msg)
                    }
                  })
                  .catch(() => {
                    reject('获取用户信息失败')
                  })
              } else {
                console.log('登录失败', result.code)
                reject(result.msg)
              }
            })
          } catch (err) {
            reject('登录出错')
          }
        } else {
          reject('获取微信登录凭证失败')
        }
      },
    })
  })
}

/**
 * 获取用户列表
 * @param {number} page_no 页码
 * @param {number} page_size 条数
 * @param {object} filterOptions
 */
export const getUserList = async (page_no = 1, page_size = 10, filterOptions) => {
  const body = { page_no, page_size }
  Object.assign(body, filterOptions)
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_USER_LIST, body)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve([])
        }
      })
      .catch(() => {
        reject(null)
      })
  })
}

/**
 * 获取用户字段参数配置列表
 */
export const getUserFieldConfig = async () => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_USER_FIELD_CONFIG, {})
      .then((result: any) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch(() => {
        reject(null)
      })
  })
}

/**
 * 获取个人信息
 */
export const getPersonalInformation = async () => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_PERSONAL_INFO, {})
      .then((result: any) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch(() => {
        reject(null)
      })
  })
}
/**
 * 编辑个人信息
 */
export const userEdit = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.UPDATE_PERSONAL_INFO, data)
      .then((result: any) => {
        if (result.code === 10000) {
          resolve(result)
        } else {
          resolve(null)
        }
      })
      .catch(() => {
        reject(null)
      })
  })
}

/**
 * 获取活动列表
 */
export const getActivityList = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_ACTIVITY_LIST, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
/**
 * 获取访问记录
 */
export const getAccessList = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_PERSONAL_ACCESS_LIST, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
/**
 * 获取收藏记录
 */
export const getCollectList = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_COLLECT_LIST, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
/**
 * 获取拉手记录
 */
export const getHandList = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_HAND_LIST, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
/**
 * 添加会员订单信息
 */
export const ordersAdd = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.ADD_ORDER, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
/**
 * 小程序支付
 */
export const pay = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.PAY, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}


/**
 * 添加拉手记录
 */
export const handAdd = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.ADD_HAND_INFO, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
/**
 * 添加会员订单信息
 */
export const pubInfo = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_PUBLIC_INFO, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
/**
 * 广告信息
 * 0：首页信息流会员广告；1个人中心会员广告，2微信群
 */
export const advestList = async (data:any) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_AD_LIST, data)
      .then((result) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}









/**
 * 获取用户信息
 * @param {number} id 用户ID
 */
export const getUserInformation = (id: number) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_USER_INFO, { to_user_id: id })
      .then((result: any) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch(() => {
        reject(null)
      })
  })
}
/**
 * 收藏或者取消收藏
 * @param {number} id 用户ID
 */
export const collectAdd = (id: number) => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.UPDATE_COLLECT, { to_user_id: id })
      .then((result: any) => {
        if (result.code === 10000) {
          resolve(result)
        } else {
          resolve(null)
        }
      })
      .catch(() => {
        reject(null)
      })
  })
}


export const getVIPConfig = async () => {
  return new Promise((resolve, reject) => {
    Request.post(URL_MAP.GET_VIP_CONFIG, {})
      .then((result: any) => {
        if (result.code === 10000) {
          resolve(result.data)
        } else {
          resolve(null)
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
